package repository

import (
	"dataaysn/internal/model"

	"gorm.io/gorm"
)

type DataSourceRepository interface {
	CreateDataSource(ds *model.DataSource) error
	GetDataSource(id string) (*model.DataSource, error)
	UpdateDataSource(ds *model.DataSource) error
	DeleteDataSource(id string) error
	ListDataSources() ([]model.DataSource, error)
	TestDataSourceConnection(ds *model.DataSource) error
}
type gormDataSourceRepository struct {
	db *gorm.DB
}

func NewGormDataSourceRepository(db *gorm.DB) DataSourceRepository {
	return &gormDataSourceRepository{db: db}
}

func (r *gormDataSourceRepository) CreateDataSource(ds *model.DataSource) error {
	return r.db.Create(ds).Error
}

func (r *gormDataSourceRepository) GetDataSource(id string) (*model.DataSource, error) {
	var ds model.DataSource
	err := r.db.First(&ds, id).Error
	return &ds, err
}

func (r *gormDataSourceRepository) UpdateDataSource(ds *model.DataSource) error {
	return r.db.Save(ds).Error
}

func (r *gormDataSourceRepository) DeleteDataSource(id string) error {
	return r.db.Delete(&model.DataSource{}, id).Error
}

func (r *gormDataSourceRepository) ListDataSources() ([]model.DataSource, error) {
	var dss []model.DataSource
	err := r.db.Find(&dss).Error
	return dss, err
}

func (r *gormDataSourceRepository) TestDataSourceConnection(ds *model.DataSource) error {
	// 实现数据源连接测试逻辑
	return nil
}
